package edu.gatech.cs4400.spring2014.A8.view;

import java.awt.GridLayout;
import java.sql.Time;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JComboBox;
import javax.swing.JPanel;

public class Availability {
	private static ArrayList<String> days;
	private static ArrayList<String> timestrings;
	private static Map<String, Time> string2time;
	private static Map<Time, String> time2string;
	private static boolean initialized = false;
	
	private Time from;
	private Time to;
	private String day;
	
	private AvailabilityPanel panel;
	
	public Availability() {
		initialize();
		to = string2time.get(timestrings.get(1));
		from = string2time.get(timestrings.get(1));
		day = days.get(1);
	}
	
	public Availability(Time from, Time to, String day) {
		initialize();
		this.from = from;
		this.to = to;
		if (days.contains(day)) {
			this.day = day;
		}
		else {
			this.day = days.get(1);
		}
	}
	
	public Time getFrom() {return from;}
	public Time getTo() {return to;}
	public String getDay() {return day;}
	
	
	public JPanel toPanel() {
		panel = new AvailabilityPanel();
		
		return panel;
	}
	
	public boolean updateFromPanel() {
		boolean result = false;
		if (panel != null) {
			day = (String)panel.daybox.getSelectedItem();
			from = string2time.get(panel.frombox.getSelectedItem());
			to = string2time.get(panel.tobox.getSelectedItem());
		}
		return result;
	}
	
	private static void initialize() {
		if (!initialized) {
			String[] daysInit = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
			days = new ArrayList<String>();
			for (String day : daysInit) {
				days.add(day);
			}
			timestrings = new ArrayList<String>();
			string2time = new HashMap<String, Time>();
			time2string = new HashMap<Time, String>();
			for (float i=6; i<22; i+=.5) {
				Time time = new Time((int)(i*60*60*1000));
				String ampm = "am";
				int hour = (int)i;
				float partial = i - hour;
				if (hour>12) {
					ampm = "pm";
					hour-=12;
				}
				if (hour==0) { 
					hour = 12;
				}
				String newtime = String.format("%d:%02d %s", hour, (int)(partial*60), ampm);
				timestrings.add(newtime);
				string2time.put(newtime, time);
				time2string.put(time, newtime);
			}			
			
			initialized = true;
		}
	}
	
	private class AvailabilityPanel extends JPanel {
		private static final long serialVersionUID = 1L;
		private JComboBox<String> daybox;
		private JComboBox<String> frombox;
		private JComboBox<String> tobox;
		
		public AvailabilityPanel() {
			daybox = new JComboBox<String>();
			for (String day : days) {
				daybox.addItem(day);
			}
			frombox = new JComboBox<String>();
			tobox = new JComboBox<String>();
			for (String time : timestrings) {
				frombox.addItem(time);
				tobox.addItem(time);
			}
			
			daybox.setSelectedItem(day);
			frombox.setSelectedItem(time2string.get(from));
			tobox.setSelectedItem(time2string.get(to));
			
			JPanel innerPanel = new JPanel();
			innerPanel.setLayout(new GridLayout(1,3));
			innerPanel.add(daybox);
			innerPanel.add(frombox);
			innerPanel.add(tobox);
			
			
			this.add(innerPanel);
		}
		
	}
	
}
